package com.forlove.app.common.widget.recyview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.forlove.app.R;
import com.forlove.app.common.base.mvp.presenter.IBasePresenter;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * 封装的医美的可以继续加载更多的
 * recyclerview adapter
 * Created by yjl on 2017/8/1.
 */
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<ViewHolder> {
    public static final int VIEW_TYPE_CONTENT = 0;  //正文页面
    public static final int VIEW_TYPE_LOAD_MORE = 1; //加载页面
    public static final int VIEW_TYPE_EMPTY = 2; //空页面
    protected Context mContext;
    protected List<T> mDatas;
    protected IBasePresenter basePresenter;
    protected FooterView mfooterView;
    protected int ItemViewId;
    protected View emptyView;
    protected OnItemClickListener mOnItemClickListener;
    private boolean isShowEmpty = true;

    public BaseRecyclerAdapter(Context context, int itemView, List<T> mDatas) {
        initAdapter(context, mDatas);
        this.ItemViewId = itemView;
    }


    public BaseRecyclerAdapter(Context context, int itemView, List<T> mDatas, IBasePresenter presenter) {
        this.ItemViewId = itemView;
        WeakReference<IBasePresenter> basePresenterWeakReference = new WeakReference<IBasePresenter>(presenter);
        this.basePresenter = basePresenterWeakReference.get();
        initAdapter(context, mDatas);
    }


    private void initAdapter(Context context, List<T> mDatas) {
        WeakReference<Context> contextWeakReference = new WeakReference<Context>(context);
        this.mContext = contextWeakReference.get();
        if (this.mDatas == null) {
            this.mDatas = new ArrayList<>();
        }
        if (mDatas != null) {
            this.mDatas.clear();
            this.mDatas.addAll(mDatas);
        }
        mfooterView = new FooterView(mContext);
    }

    public void setPresenter(IBasePresenter presenter) {
        this.basePresenter = presenter;
    }

    /**
     * 设置数据并刷新
     * 更新数据必须要用这个
     *
     * @param datas
     */
    public synchronized void updateDatas(List<T> datas) {
        if (this.mDatas != null) {
            mDatas.clear();
            this.mDatas.addAll(datas);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if ((mDatas == null || mDatas.size() == 0) && isShowEmpty) {
            return VIEW_TYPE_EMPTY;
        } else if (position >= mDatas.size()) {
            return VIEW_TYPE_LOAD_MORE;
        } else if (mDatas != null && mDatas.size() > 0
                && position < mDatas.size()) {
            return VIEW_TYPE_CONTENT;
        }
        return VIEW_TYPE_CONTENT;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == VIEW_TYPE_CONTENT) {
            if (ItemViewId <= 0) {
                throw new NullPointerException("ItemViewId must not be equals 0!");
            } else {
                view = LayoutInflater.from(mContext).inflate(ItemViewId, parent, false);
            }
        } else if (viewType == VIEW_TYPE_LOAD_MORE) {
            view = mfooterView;
        } else if (viewType == VIEW_TYPE_EMPTY) {
            if (emptyView == null) {
                emptyView = LayoutInflater.from(mContext).inflate(R.layout.layout_no_data, parent, false);
            }
            view = emptyView;
        }
        ViewHolder holder = ViewHolder.createViewHolder(parent.getContext(), view);
        setListener(holder, viewType);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (mDatas == null || mDatas.size() == 0 || position >= mDatas.size()) {
            return;
        }
        convertView(holder, mDatas.get(position), position);
    }

    protected abstract void convertView(ViewHolder holder, T entity, int position);

    @Override
    public int getItemCount() {
        if (mDatas == null || mDatas.size() == 0) {
            if (isShowEmpty) {
                return 1;
            } else {
                return 0;
            }
        } else {
            if (isShowEmpty) {
                return mDatas.size() + 1;
            } else {
                return mDatas.size();
            }
        }
    }

    protected boolean isEnabled(int viewType) {
        if (viewType == VIEW_TYPE_CONTENT) {
            return true;
        }
        return false;
    }

    protected void setListener(final ViewHolder viewHolder, int viewType) {
        if (!isEnabled(viewType)) {
            return;
        }
        viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    int position = viewHolder.getAdapterPosition();
                    mOnItemClickListener.onItemClick(v, viewHolder, position);
                }
            }
        });

        viewHolder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemClickListener != null) {
                    int position = viewHolder.getAdapterPosition();
                    return mOnItemClickListener.onItemLongClick(v, viewHolder, position);
                }
                return false;
            }
        });
    }

    /**
     * 显示底部正在加载状态
     */
    public void showFootLoading() {
        if (mfooterView != null) {
            mfooterView.showLoading();
        }
    }

    /**
     * 显示底部没有更多数据状态
     */
    public void showNoMoreData() {
        if (mfooterView != null) {
            mfooterView.showNomoreData();
        }
    }

    /**
     * 显示加载完成状态
     */
    public void showLoadCompelte() {
        if (mfooterView != null) {
            mfooterView.showLoadFinish();
        }
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, RecyclerView.ViewHolder holder, int position);

        boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position);
    }

    public void hideFooterView() {
        if (mfooterView != null) {
            mfooterView.setVisibility(View.GONE);
        }
    }

    public void hideEmptyView() {
        if (emptyView != null) {
            emptyView.setVisibility(View.GONE);
        }
        isShowEmpty = false;
    }

    public T getPositionData(int position) {
        if (mDatas != null && mDatas.size() > position) {
            return mDatas.get(position);
        }
        return null;
    }
}
